package com.treasury.glasses.service;

import com.treasury.glasses.Main;
import com.treasury.glasses.domain.ActivityLog;
import com.treasury.glasses.domain.ProgramUser;
import com.treasury.glasses.persistence.DataSource;

/**
 * Service class for the SceneController.
 * @author <a href="mailto:glasses@audienz.eu">Jean-Luc Burot</a>
 */

public class SceneService {
    private final DataSource dataSource = DataSource.getInstance();
    
    /**
     * Creates an ActivityLog.
     * @param activityLog ActivityLog
     */
    public final void createActivityLog(final ActivityLog activityLog) {
        this.dataSource.save(activityLog);
    }
    
    /**
     * Creates a ProgramUser.
     * @param programUser ProgramUser
     */
    public final void createProgramUser(final ProgramUser programUser) {
        this.dataSource.save(programUser);
    }
    
    /**
    * Checks in the persistence whether the given username is correct.
    * @param username Username
    * @return ProgramUser when correct.
    */
    public final ProgramUser checkUsername(final String username) {
        try {
            return Main.getEntityManager()
                       .createNamedQuery(ProgramUser.CHECK_USER, ProgramUser.class)
                       .setParameter("username", username)
                       .getSingleResult();
        } catch (RuntimeException ex) {
            System.out.println("Wrong username or password.");
            return null;
        }
    }
}
